package richbar.com.richbar.activity.product;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.common.base.BaseActivity;
import com.common.help.RecycleViewHelp;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.bean.GoodsListResult;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.pop.FilterPop;

/**
 * Created by QiYang on 2018/10/23.
 */

public class ProductListAc extends BaseActivity {
    @BindView(R.id.comprehensive)
    TextView comprehensive;
    @BindView(R.id.sales_volume)
    TextView salesVolume;
    @BindView(R.id.price_tv)
    TextView priceTv;
    @BindView(R.id.price_up_iv)
    ImageView priceUpIv;
    @BindView(R.id.price_down_iv)
    ImageView priceDownIv;
    @BindView(R.id.price_body)
    LinearLayout priceBody;
    @BindView(R.id.filter)
    TextView filter;
    @BindView(R.id.input_et)
    EditText inputEt;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.swipe)
    SwipeRefreshLayout swipe;

    private MyAdapter adapter;
    private String priceMin = "";
    private String priceMax = "";
    private String sortType = "1";

    private boolean isUpPrice = false;
    private String goodsName;

    @Override
    public int initLayout() {
        return R.layout.ac_product;
    }

    @Override
    public void initTitle() {

    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        priceUpIv.setImageResource(R.mipmap.price_up_black);
        priceDownIv.setImageResource(R.mipmap.price_down_black);
        swipe.setColorSchemeResources(R.color.blue);
        inputEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_SEARCH){
                    getData(false);
                    hideSoftInput();
                }
                return false;
            }
        });
    }

    @Override
    public void initData() {
        LogUtil.e("KKKKKKKKKK",1);
        if (null != getBundle()){
            LogUtil.e("KKKKKKKKKK",2);
            goodsName = getBundle().getString("goodsName");
            inputEt.setHint(goodsName);
            getData(false);
            hideSoftInput();
        }
        RecycleViewHelp.setGridMannager(recycleView,6,2,this);
        adapter = new MyAdapter(R.layout.item_goods_list,new ArrayList<GoodsListResult.DataEntity.DataBean>());
        recycleView.setAdapter(adapter);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                getData(true);
            }
        });
        swipe.setRefreshing(true);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                LogUtil.e("KKKKKKKKKK",3);
                getData(false);
            }
        });
    }

    private void getData(final boolean loadMore) {
        if (loadMore){
            pageAdd();
        }else {
            pageReset();
        }
        String temp = StringUtil.getStr(inputEt);
        if (!StringUtil.isEmpty(temp)){
            goodsName = temp;
        }
        NetWorkUtil.getList(page + "", "10", goodsName, sortType, priceMin, priceMax, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final GoodsListResult result = MyGson.getInstance().fromJson(data, GoodsListResult.class);
                if (checkIsOk(result)){
                    setData(result,loadMore);
                    refreshDismiss(swipe);
                }
            }
        });
    }

    private void setData(GoodsListResult result,boolean loadMore) {
        List<GoodsListResult.DataEntity.DataBean> data = result.getData().getRows();
        if (data == null || data.size() == 0){

        }else {
            if (loadMore){
                data.addAll(data);
            }
            adapter.setNewData(data);
        }
    }


    @OnClick({R.id.comprehensive, R.id.sales_volume, R.id.price_body, R.id.filter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.comprehensive:
                sortType = "1";
                comprehensive.setTextColor(ContextCompat.getColor(this,R.color.red));
                salesVolume.setTextColor(ContextCompat.getColor(this,R.color.black));
                priceTv.setTextColor(ContextCompat.getColor(this,R.color.black));
                priceUpIv.setImageResource(R.mipmap.price_up_black);
                priceDownIv.setImageResource(R.mipmap.price_down_black);
                getData(false);
                break;
            case R.id.sales_volume:
                sortType = "2";
                comprehensive.setTextColor(ContextCompat.getColor(this,R.color.black));
                salesVolume.setTextColor(ContextCompat.getColor(this,R.color.red));
                priceTv.setTextColor(ContextCompat.getColor(this,R.color.black));
                priceUpIv.setImageResource(R.mipmap.price_up_black);
                priceDownIv.setImageResource(R.mipmap.price_down_black);
                getData(false);
                break;
            case R.id.price_body:
                comprehensive.setTextColor(ContextCompat.getColor(this,R.color.black));
                salesVolume.setTextColor(ContextCompat.getColor(this,R.color.black));
                priceTv.setTextColor(ContextCompat.getColor(this,R.color.red));
                if (isUpPrice){
                    sortType = "4";
                    isUpPrice = false;
                    priceUpIv.setImageResource(R.mipmap.price_up_black);
                    priceDownIv.setImageResource(R.mipmap.price_down_red);
                }else {
                    sortType = "3";
                    isUpPrice = true;
                    priceUpIv.setImageResource(R.mipmap.price_up_red);
                    priceDownIv.setImageResource(R.mipmap.price_down_black);
                }
                getData(false);
                break;
            case R.id.filter:
                showFilter();
                break;
        }
    }

    private void showFilter() {
        FilterPop pop = new FilterPop(this);
        pop.showFilter(filter, new FilterPop.Callback() {
            @Override
            public void onFilter(String minPrice, String maxPrice) {
                priceMin = minPrice;
                priceMax = maxPrice;
                getData(false);
            }
        });
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }


    private class MyAdapter extends BaseQuickAdapter<GoodsListResult.DataEntity.DataBean> {
        public MyAdapter(int layoutResId, List<GoodsListResult.DataEntity.DataBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, final GoodsListResult.DataEntity.DataBean bean) {
            holder.setText(R.id.name,bean.getGoods_name());
            holder.setText(R.id.price,bean.getGoods_price());
            holder.setText(R.id.comment,bean.getGoods_rate() + "条评价");
            ImageView iv = holder.getView(R.id.iv);
            GlideUtil.loadImage(mContext,NetWorkUtil.URL + bean.getGoods_img(),iv);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    start(ProductDetailsAc.class, new OnBundleListener() {
                        @Override
                        public Bundle onSetBundle(Bundle bundle) {
                            bundle.putString("id",bean.getGoods_id());
                            bundle.putString("pic",bean.getGoods_img());
                            return bundle;
                        }
                    });
                }
            });
        }
    }
}
